// Fill out your copyright notice in the Description page of Project Settings.


#include "Core/GameCore/TowerDefencePlayerController.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "Core/GameCore/TowerDefenceGameCamera.h"

ATowerDefencePlayerController::ATowerDefencePlayerController()
{
	bShowMouseCursor = true;
}

void ATowerDefencePlayerController::Tick(float DeltaSeconds)
{
	Super::Tick(DeltaSeconds);

	ScreenMoveUnits.ListenScreenMove(this, 10.0f);
}

void ATowerDefencePlayerController::BeginPlay()
{
	Super::BeginPlay();

	SetInputModeAndUI();
}

void ATowerDefencePlayerController::SetInputModeAndUI()
{
	FInputModeGameAndUI InputMode;
	InputMode.SetLockMouseToViewportBehavior(EMouseLockMode::LockAlways);
	InputMode.SetHideCursorDuringCapture(false);

	SetInputMode(InputMode);
}

void ATowerDefencePlayerController::SetupInputComponent()
{
	Super::SetupInputComponent();

	// 添加 mapping映射
	const ULocalPlayer* LocalPlayer = GetLocalPlayer();
	UEnhancedInputLocalPlayerSubsystem* InputSystem = LocalPlayer->GetSubsystem<UEnhancedInputLocalPlayerSubsystem>();
	InputSystem->AddMappingContext(InputMappingContext, 1);
	// 绑定动作事件
	UEnhancedInputComponent* Input = Cast<UEnhancedInputComponent>(InputComponent);
	Input->BindAction(IA_MouseScroll, ETriggerEvent::Started, this, &ATowerDefencePlayerController::MouseWheel);
}

void ATowerDefencePlayerController::MouseWheel(const FInputActionInstance& Instance)
{
	const float Value = Instance.GetValue().Get<float>();

	ATowerDefenceGameCamera* Camera =  Cast<ATowerDefenceGameCamera>(GetPawn());
	Camera->Zoom(Value, 15.f);
}
